#!/usr/bin/env Python
# -*- coding: utf-8 -*-

"""
使用requests请求代理服务器
请求http和https网页均适用
"""

import requests
def get_proxy():
    # 提取代理API接口，获取1个代理IP
    api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=otwdv6w8ymhmzacbrm89&signature=kqcm84shodw4sf74proc45vn8g2sry40&num=1&sep=1"

    # 获取API接口返回的代理IP
    proxy_ip = requests.get(api_url).text

    # 用户名密码认证(私密代理/独享代理)
    username = "d4715109620"
    password = "c1rb39fm"
    proxies = {
        "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
        "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
    }

    # 白名单方式（需提前设置白名单）
    # proxies = {
    #     "http": "http://%(proxy)s/" % {"proxy": proxy_ip},
    #     "https": "http://%(proxy)s/" % {"proxy": proxy_ip}
    # }

    # 要访问的目标网页


    # 使用代理IP发送请求
    return proxies

def get_proxy_poll(num):
    proxy_pull = []
    for i in range(1,num):
        proxy = get_proxy()
        print("获取代理", proxy)
        proxy_pull.append(proxy)


    return  proxy_pull


def get_tunnel():
    # 隧道域名:端口号
    tunnel = "p1000.kdltps.com:15818"

    # 用户名密码方式
    username = "t16308820803840"
    password = "x99zuz0y"
    proxies = {
        "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
        "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
    }

    # 白名单方式（需提前设置白名单）
    # proxies = {
    #     "http": "http://%(proxy)s/" % {"proxy": tunnel},
    #     "https": "http://%(proxy)s/" % {"proxy": tunnel}
    # }

    # 要访问的目标网页
    target_url = "https://dev.kdlapi.com/testproxy"

    # 使用隧道域名发送请求
    return proxies